//
//  MyAccountViewController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2024/8/20.
//  Copyright © 2024 GCY. All rights reserved.
//

#import "MyAccountViewController.h"
#import "MyAccountTableViewCell.h"
#import "AccountInformationViewController.h"

@interface MyAccountViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@end
static NSString *const cellID = @"MyAccountTableViewCell";

static const CGFloat cellHeight = 148;

@implementation MyAccountViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的账户";
    
    _pageNum = 1;
    
    [self setupBaseView];
  
    
}
- (void)setupBaseView {
        
    self.view.backgroundColor = BackgroundColor;
    
    [self.view addSubview:self.tableView];
}
- (void)tradeRecordsListRequest {
    
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params;
    NSString *urlString = @"";
    params = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": DEFULT_LOAD_NUM,
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    

    urlString = CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], PERSONUSERAMOUT);
    [HTTPManager getNetworkRequest:urlString withParameter:params withVC:self netName:@"账户分账列表" success:^(id response, NSString *msg) {
        
        if ([response[@"userAccounts"] count] != 0) {
        
        [weakSelf.dataArray addObjectsFromArray:response[@"userAccounts"]];
            NSLog(@"%@",weakSelf.dataArray);
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        weakSelf.foundImageView.hidden = YES;
        weakSelf.tableView.hidden = NO;

        } else {
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}
//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self tradeRecordsListRequest];
}

- (void)loadMoreData{
    _pageNum ++;
    [self tradeRecordsListRequest];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
    MyAccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyAccountTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell fuzhiUI:self.dataArray[indexPath.row]];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AccountInformationViewController *aivc = [[AccountInformationViewController alloc] init];
    NSDictionary *dic = self.dataArray[indexPath.row];
    aivc.typeStr = CJStringWithFormat(@"%@",dic[@"accountType"]);
    aivc.dataDic = dic;
    [self.navigationController pushViewController:aivc animated:YES];
}

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - MenuBarHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyAccountTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"MyAccountTableViewCell"];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
//        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.view.center;
        _foundImageView.centerY -= 60;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}


@end
